import 'package:erp/widgets/form1/form_builder.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class FilterChipItem<T> {
  final Widget label;
  final Widget? avatar;
  final T data;
  final EdgeInsets? contentPadding;
  final EdgeInsets? labelPadding;
  final EdgeInsets padding;
  final bool readOnly;
  final bool visible;

  FilterChipItem(
      {required this.label,
      this.avatar,
      required this.data,
      EdgeInsets? padding,
      this.readOnly = false,
      this.visible = true,
      this.contentPadding = EdgeInsets.zero,
      this.labelPadding})
      : this.padding = padding ?? EdgeInsets.symmetric(horizontal: 21.w, vertical: 0);
}

class FilterChipFormField<T> extends BaseFormField {
  final String name;
  final String? label;
  final String? Function(dynamic value)? validator;
  final List<FilterChipItem<T>> items;

  FilterChipFormField({
    required this.name,
    this.label,
    this.validator,
    required this.items,
  }) : super(name: name, label: label, validator: validator, labelPosition: LabelPosition.top);

  @override
  BaseFormFieldState createState() {
    return _FilterChipFormFieldState();
  }
}

class _FilterChipFormFieldState<T> extends BaseFormFieldState<List<T>> {
  List<T> get initialValue {
    if (initValueMap != null && initValueMap!.containsKey(name)) {
      return initValueMap![name] ?? [];
    }
    return [];
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> chips = [];
    for (FilterChipItem<T> item in (widget as FilterChipFormField<T>).items) {
      bool _readOnly = false;
      if (initStateMap != null && initStateMap!.containsKey('readOnly')) {
        _readOnly = initStateMap!['readOnly']!;
      }
      bool _selected = value.contains(item.data);
      FilterChip chip = FilterChip(
        showCheckmark: false,
        selected: _selected,
        label: item.label,
        avatar: item.avatar,
        padding: item.contentPadding,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.r))),
        backgroundColor: Color(0xffFAFAFA),
        selectedColor: Color(0xff4C88FF),
        side: _selected ? BorderSide.none : BorderSide(color: Color(0xffE6E6E6), width: 1.w, style: BorderStyle.solid),
        labelStyle: TextStyle(fontSize: 26.sp, color: _selected ? Colors.white : Color(0xffCCCCCC)),
        onSelected: _readOnly
            ? (bool selected) {}
            : (bool selected) {
                if (selected) {
                  setState(() {
                    value.add(item.data);
                  });
                } else {
                  setState(() {
                    value.remove(item.data);
                  });
                }
              },
      );
      chips.add(
        Padding(
          padding: EdgeInsets.only(right: 21.w),
          child: chip,
        ),
      );
    }

    return labelWrapper(
      child: Container(
        width: MediaQuery.of(context).size.width,
        child: Wrap(
          children: [...chips],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    value = [];
  }

  void initFormManagement() {
    super.initFormManagement();
    this.value = initialValue;
  }

  void dispose() {
    super.dispose();
  }
}
